import Mock from 'mockjs';

// 模拟头饰分类数据
const categories = [
  {
    id: 1,
    name: '帽子',
    sort: 1,
    description: '各种帽子类型',
    createTime: '2024-01-01 10:00:00'
  },
  {
    id: 2,
    name: '发饰',
    sort: 2,
    description: '发夹、发带等',
    createTime: '2024-01-01 10:00:00'
  },
  {
    id: 3,
    name: '头巾',
    sort: 3,
    description: '各种头巾样式',
    createTime: '2024-01-01 10:00:00'
  },
  {
    id: 4,
    name: '皇冠',
    sort: 4,
    description: '皇冠类装饰',
    createTime: '2024-01-01 10:00:00'
  }
];

// 模拟头饰数据
const headwearList = [
  {
    id: 1,
    name: '可爱猫耳帽',
    imageUrl: 'https://cdn.stite.net/plp/headwear1.jpg',
    categoryId: 1,
    categoryName: '帽子',
    isRecommended: true,
    sort: 1,
    description: '可爱的猫耳造型帽子',
    createTime: '2024-01-01 10:00:00',
    updateTime: '2024-01-01 10:00:00'
  },
  {
    id: 2,
    name: '蝴蝶结发夹',
    imageUrl: 'https://cdn.stite.net/plp/headwear2.jpg',
    categoryId: 2,
    categoryName: '发饰',
    isRecommended: false,
    sort: 2,
    description: '粉色蝴蝶结发夹',
    createTime: '2024-01-01 10:00:00',
    updateTime: '2024-01-01 10:00:00'
  },
  {
    id: 3,
    name: '运动头巾',
    imageUrl: 'https://cdn.stite.net/plp/headwear3.jpg',
    categoryId: 3,
    categoryName: '头巾',
    isRecommended: true,
    sort: 3,
    description: '运动风格头巾',
    createTime: '2024-01-01 10:00:00',
    updateTime: '2024-01-01 10:00:00'
  },
  {
    id: 4,
    name: '公主皇冠',
    imageUrl: 'https://cdn.stite.net/plp/headwear4.jpg',
    categoryId: 4,
    categoryName: '皇冠',
    isRecommended: true,
    sort: 4,
    description: '精美的公主皇冠',
    createTime: '2024-01-01 10:00:00',
    updateTime: '2024-01-01 10:00:00'
  }
];

export default [
  // 获取头饰列表
  {
    url: '/api/headwear/list',
    method: 'get',
    response: (config) => {
      const { page = 1, limit = 20, keyword = '', categoryId = '', isRecommended = '' } = config.query;

      let filteredList = [...headwearList];

      // 关键词搜索
      if (keyword) {
        filteredList = filteredList.filter(item =>
          item.name.includes(keyword) || item.description.includes(keyword)
        );
      }

      // 分类筛选
      if (categoryId) {
        filteredList = filteredList.filter(item => item.categoryId == categoryId);
      }

      // 推荐状态筛选
      if (isRecommended !== '') {
        filteredList = filteredList.filter(item => item.isRecommended == isRecommended);
      }

      // 分页
      const start = (page - 1) * limit;
      const end = start + limit;
      const paginatedList = filteredList.slice(start, end);

      return {
        code: 200,
        data: {
          list: paginatedList,
          total: filteredList.length,
          page: parseInt(page),
          limit: parseInt(limit)
        }
      };
    }
  },

  // 获取头饰详情
  {
    url: '/api/headwear/detail/:id',
    method: 'get',
    response: (config) => {
      const id = config.params.id;
      const headwear = headwearList.find(item => item.id == id);

      if (headwear) {
        return {
          code: 200,
          data: headwear
        };
      } else {
        return {
          code: 404,
          message: '头饰不存在'
        };
      }
    }
  },

  // 新增头饰
  {
    url: '/api/headwear/create',
    method: 'post',
    response: (config) => {
      const newHeadwear = {
        id: Mock.mock('@integer(1000, 9999)'),
        ...config.body,
        categoryName: categories.find(cat => cat.id == config.body.categoryId)?.name || '',
        createTime: Mock.mock('@datetime'),
        updateTime: Mock.mock('@datetime')
      };

      headwearList.push(newHeadwear);

      return {
        code: 200,
        data: newHeadwear,
        message: '创建成功'
      };
    }
  },

  // 更新头饰
  {
    url: '/api/headwear/update/:id',
    method: 'put',
    response: (config) => {
      const id = config.params.id;
      const index = headwearList.findIndex(item => item.id == id);

      if (index !== -1) {
        const updatedHeadwear = {
          ...headwearList[index],
          ...config.body,
          categoryName: categories.find(cat => cat.id == config.body.categoryId)?.name || '',
          updateTime: Mock.mock('@datetime')
        };

        headwearList[index] = updatedHeadwear;

        return {
          code: 200,
          data: updatedHeadwear,
          message: '更新成功'
        };
      } else {
        return {
          code: 404,
          message: '头饰不存在'
        };
      }
    }
  },

  // 删除头饰
  {
    url: '/api/headwear/delete/:id',
    method: 'delete',
    response: (config) => {
      const id = config.params.id;
      const index = headwearList.findIndex(item => item.id == id);

      if (index !== -1) {
        headwearList.splice(index, 1);
        return {
          code: 200,
          message: '删除成功'
        };
      } else {
        return {
          code: 404,
          message: '头饰不存在'
        };
      }
    }
  },

  // 获取头饰分类列表
  {
    url: '/api/headwear/categories',
    method: 'get',
    response: () => {
      return {
        code: 200,
        data: categories
      };
    }
  },

  // 新增头饰分类
  {
    url: '/api/headwear/category/create',
    method: 'post',
    response: (config) => {
      const newCategory = {
        id: Mock.mock('@integer(100, 999)'),
        ...config.body,
        createTime: Mock.mock('@datetime')
      };

      categories.push(newCategory);

      return {
        code: 200,
        data: newCategory,
        message: '分类创建成功'
      };
    }
  },

  // 更新头饰分类
  {
    url: '/api/headwear/category/update/:id',
    method: 'put',
    response: (config) => {
      const id = config.params.id;
      const index = categories.findIndex(item => item.id == id);

      if (index !== -1) {
        const updatedCategory = {
          ...categories[index],
          ...config.body,
          updateTime: Mock.mock('@datetime')
        };

        categories[index] = updatedCategory;

        return {
          code: 200,
          data: updatedCategory,
          message: '分类更新成功'
        };
      } else {
        return {
          code: 404,
          message: '分类不存在'
        };
      }
    }
  },

  // 删除头饰分类
  {
    url: '/api/headwear/category/delete/:id',
    method: 'delete',
    response: (config) => {
      const id = config.params.id;
      const index = categories.findIndex(item => item.id == id);

      if (index !== -1) {
        categories.splice(index, 1);
        return {
          code: 200,
          message: '分类删除成功'
        };
      } else {
        return {
          code: 404,
          message: '分类不存在'
        };
      }
    }
  }
]; 